package com.jinlia.show.common.core.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.reflect.GenericTypeUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * 仿照 com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
 * @param <S>
 * @param <M>
 * @param <T>
 */
public class BaseController<S extends ServiceImpl<M, T>, M extends BaseMapper<T>, T> {

    @Autowired
    private S serviceImpl;

    @Autowired
    private M baseMapper;


    /**
     * 分页获取数据
     *
     * @param iPage
     * @param request
     * @return
     */
    @GetMapping("list/page")
    public ResponseEntity<IPage> getPage(Page iPage, T request) {
        QueryWrapper<T> sysDeptQueryWrapper = new QueryWrapper<>(request);
        return ResponseEntity.ok(serviceImpl.page(iPage, sysDeptQueryWrapper));
    }
    /**
     * 分页获取数据
     *
     * @param request
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<T>> getPage(T request) {
        QueryWrapper<T> sysDeptQueryWrapper = new QueryWrapper<>(request);
        return ResponseEntity.ok(serviceImpl.list(sysDeptQueryWrapper));
    }
    /**
     * 获取全部数据
     *
     * @return
     */
    @GetMapping("all")
    public List<T> get() {
        Long count = serviceImpl.count();
        if (count > 100) {
            List<T> list = serviceImpl.lambdaQuery().last("limit 100").list();
            return list;
        }
        return serviceImpl.list();
    }
    /**
     * ----------------------------------------------------curl接口封装-------------------------------------------------------------
     */
    /**
     * 新增
     */
    @PostMapping("create")
    public ResponseEntity<T> create(@RequestBody T request) {
        serviceImpl.save(request);
        return ResponseEntity.ok(request);
    }

    /**
     * 新增
     */
    @PostMapping("update")
    public ResponseEntity<T> update(@RequestBody T request) {
        serviceImpl.updateById(request);
        return ResponseEntity.ok(request);
    }

    /**
     * 删除
     */
    @DeleteMapping
    public ResponseEntity<Boolean> delete(Long id) {
        return ResponseEntity.ok(serviceImpl.removeById(id));
    }


    protected final Class<?>[] typeArguments = GenericTypeUtils.resolveTypeArguments(this.getClass(), BaseController.class);
    protected final Class<S> serviceClass = this.currentServiceClass();
    protected final Class<T> entityClass = this.currentModelClass();
    protected final Class<M> mapperClass = this.currentMapperClass();
    protected Class<S> currentServiceClass() {
        return (Class<S>) this.typeArguments[0];
    }
    protected Class<M> currentMapperClass() {
        return (Class<M>) this.typeArguments[1];
    }

    protected Class<T> currentModelClass() {
        return (Class<T>) this.typeArguments[2];
    }

    public Class<S> getServiceClass() {
        return this.serviceClass;
    }
    public Class<T> getEntityClass() {
        return this.entityClass;
    }
    public Class<M> getMapperClass() {
        return this.mapperClass;
    }

    public M getBaseMapper() {
        return baseMapper;
    }

    public S getServiceImpl() {
        return serviceImpl;
    }
}
